package com.ss.android.account.c;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.account.activity.AuthorizeActivity;
import com.ss.android.common.util.ToolUtils;

import org.json.JSONObject;

import java.util.List;

public final class QZone {
    public interface a$a {
        void onComplete();

        void onError(int arg1, String arg2);

        void onCancel();
    }

    public static final class b {
        private final AuthorizeActivity a;

        public b(AuthorizeActivity arg1) {
            this.a = arg1;
        }

        public final void a() {
            if (!this.a.isDestroyed()) {
                this.a.a();
            }
        }
    }

    private static String a = "100290348";
    private static int b = 5656;
    private static boolean c = false;

    public static void a(int arg6, Intent intent, b arg8) {
        if (arg6 == -1) {
            if (intent == null) {
                arg8.a();
                return;
            }

            if (intent.getIntExtra("key_error_code", 0) == 0) {
                String v0 = intent.getStringExtra("key_response");
                try {
                    JSONObject v1 = new JSONObject(v0);
                    String v3 = v1.getString("access_token");
                    String v4 = v1.getString("expires_in");
                    String v5 = v1.getString("openid");
                    if (TextUtils.isEmpty(((CharSequence) v3))) {
                    } else if (TextUtils.isEmpty(((CharSequence) v4))) {
                    } else if (TextUtils.isEmpty(((CharSequence) v5))) {
                    } else {
                        Logger.d("AuthorizeActivity", "qzone sso complete: " + v3 + " " + v4 + " " + v5);
                        if (arg8.a.isDestroyed()) {
                            return;
                        }

                        AuthorizeActivity v1_1 = arg8.a;
                        v1_1.c = true;
                        v1_1.a.a(((Context) v1_1), v1_1.b, v3, v4, v5);
                        return;
                    }
                } catch (Exception exception) {
                    Log.w("QZone", "parse sso reponse exception: " + exception);
                }

                arg8.a();
                return;
            }

            intent.getStringExtra("key_error_msg");
            intent.getStringExtra("key_error_detail");
            arg8.a();
            return;
        }

        if (arg6 == 0 && !arg8.a.isDestroyed()) {
            arg8.a.a();
        }
    }

    public static boolean a(Activity arg5, String arg6, boolean arg7) {
        boolean v0;
        if (TextUtils.isEmpty(a)) {
            v0 = false;
            return v0;
        }

        Bundle v2 = new Bundle();
        v2.putString("scope", arg6);
        v2.putString("client_id", a);
        Intent v3 = new Intent();
        String v0_1 = arg7 ? "com.qzone" : "com.tencent.mobileqq";
        v3.setClassName(v0_1, "com.tencent.open.agent.AgentActivity");
        v3.putExtra("key_action", "action_login");
        v3.putExtra("key_params", v2);
        try {
            List v0_4 = arg5.getPackageManager().queryIntentActivities(v3, 65536);
            if (v0_4 != null && !v0_4.isEmpty()) {
            } else {
                return false;
            }
            int v0_5 = 32974;
            arg5.startActivityForResult(v3, v0_5);
            v0 = true;
        } catch (ActivityNotFoundException v0_2) {
            label_39:
            v0 = false;
        } catch (Exception v0_3) {
            label_48:
            Log.w("QZone", "try qzone sso exception: " + v0_3);
            v0 = false;
        }

        return v0;
    }

    private static int a(String arg8, String arg9) {
        int v0 = -1;
        if (arg8 != null) {
            String[] v4 = arg8.split("\\.");
            String[] v5 = arg9.split("\\.");
            int v3 = 0;
            try {
                while (v3 < v4.length) {
                    if (v3 >= v5.length) {
                        break;
                    }

                    int v6 = Integer.parseInt(v4[v3]);
                    int v7 = Integer.parseInt(v5[v3]);
                    if (v6 < v7) {
                        return v0;
                    } else if (v6 > v7) {
                        return 1;
                    } else {
                        ++v3;
                        continue;
                    }
                }

                if (v4.length > v5.length) {
                    return 1;
                }

                if (v4.length < v5.length) {
                    return v0;
                }
            } catch (NumberFormatException v0_1) {
                return arg8.compareTo(arg9);
            }

            v0 = 0;
        }

        return v0;
    }

    private static int a(boolean arg2) {
        int v0 = b + 1;
        b = v0;
        if (v0 > 6656) {
            b = 5656;
        }

        c = arg2;
        return b;
    }

    public static String a() {
        return a;
    }

    public static boolean onShareToQQResult(int arg1, int arg2, Intent arg3, a$a arg4) {
        return onShareToQQResult(arg1, arg2, arg3, arg4, false);
    }

    public static boolean onShareToQQResult(int arg7, int arg8, Intent arg9, a$a arg10, boolean arg11) {
        boolean v0 = false;
        Logger.d("QZone", "shareToQQ result " + b + " " + arg7 + " " + arg8 + " " + arg11);
        if (arg7 >= 5656 && arg7 <= 6656 && arg7 == b && arg11 == c) {
            if (arg8 == 0) {
                if (arg10 != null) {
                    arg10.onCancel();
                }
                v0 = true;
            } else {
                if (arg8 == -1 && arg9 != null) {
                    int v0_1 = arg9.getIntExtra("key_error_code", 0);
                    if (v0_1 == 0) {
                        Logger.d("QZone", "shareToQQ ok " + arg9.getStringExtra("key_response"));
                        if (arg10 != null) {
                            arg10.onComplete();
                        }

                        v0 = true;
                    } else {
                        String v2 = arg9.getStringExtra("key_error_msg");
                        String v3 = arg9.getStringExtra("key_error_detail");
                        Logger.d("QZone", "shareToQQ error " + v2 + " " + v3);
                        if (arg10 != null) {
                            arg10.onError(v0_1, v3);
                        }

                        v0 = true;
                    }

                    return v0;
                }

                v0 = true;
            }
        }

        return v0;
    }

    public static boolean a(Context arg3) {
        boolean v0 = false;
        String v1 = c(arg3);
        if (!android.support.design.a.isTextEmpty(v1) && a(v1, "4.1") >= 0) {
            v0 = true;
        }

        return v0;
    }

    public static boolean a(Context arg8, String arg9, String arg10, String arg11, String arg12, String arg13, String arg14) {
        return a(arg8, arg9, arg10, arg11, arg12, arg13, arg14, 1);
    }

    private static boolean a(Context arg1, String arg2, String arg3, String arg4, String arg5, String arg6, String arg7, int arg8) {
        return b(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
    }

    private static boolean b(Context arg7, String arg8, String arg9, String arg10, String arg11, String arg12, String arg13, int arg14) {
        Intent v1_3;
        int v3 = 80;
        int v2 = 40;
        int v5 = 20;
        int v4 = 2;
        boolean v0 = false;
        if (!android.support.design.a.isTextEmpty(arg11) && !TTUtils.isValidUrl(arg11)) {
            arg11 = "";
        }

        if (!android.support.design.a.isTextEmpty(arg8) && !TTUtils.isValidUrl(arg8)) {
            arg8 = "";
        }

        if ((!android.support.design.a.isTextEmpty(arg9) || !android.support.design.a.isTextEmpty(arg10) || !android.support.design.a.isTextEmpty(arg11)) && !android.support.design.a.isTextEmpty(arg8)) {
            if (!android.support.design.a.isTextEmpty(arg9) && arg9.length() > v2) {
                arg9 = arg9.substring(0, v2) + "...";
            }

            if (!android.support.design.a.isTextEmpty(arg10) && arg10.length() > v3) {
                arg10 = arg10.substring(0, v3) + "...";
            }

            StringBuilder v1 = new StringBuilder("mqqapi://share/to_fri?src_type=app&version=1&file_type=news");
            if (!android.support.design.a.isTextEmpty(arg11)) {
                v1.append("&image_url=").append(Base64.encodeToString(arg11.getBytes(), v4));
            }

            if (!android.support.design.a.isTextEmpty(arg12)) {
                v1.append("&file_data=").append(Base64.encodeToString(arg12.getBytes(), v4));
            }

            if (!android.support.design.a.isTextEmpty(arg9)) {
                v1.append("&title=").append(Base64.encodeToString(arg9.getBytes(), v4));
            }

            if (!android.support.design.a.isTextEmpty(arg10)) {
                v1.append("&description=").append(Base64.encodeToString(arg10.getBytes(), v4));
            }

            v1.append("&share_id=").append(a);
            if (!android.support.design.a.isTextEmpty(arg8)) {
                v1.append("&url=").append(Base64.encodeToString(arg8.getBytes(), v4));
            }

            if (!android.support.design.a.isTextEmpty(arg13)) {
                if (arg13.length() > v5) {
                    arg13 = arg13.substring(0, v5) + "...";
                }

                v1.append("&app_name=").append(Base64.encodeToString(arg13.getBytes(), v4));
            }

            v1.append("&req_type=" + Base64.encodeToString(String.valueOf(arg14).getBytes(), v4));
            String v1_1 = v1.toString();
            Logger.v("shareToQQ", v1_1);
            Bundle v2_1 = new Bundle();
            v2_1.putString("scheme", v1_1);
            Bundle v3_1 = new Bundle(v2_1);
            v3_1.putString("appid", a);
            v3_1.putString("platform", "androidqz");
            v3_1.putString("pf", "openmobile_android");
            String v4_1 = "action_share_qq";
            Intent v2_2 = null;
            try {
                v1_3 = new Intent("android.intent.action.VIEW", Uri.parse(v1_1));
                v1_3.putExtra("key_request_code", a(false));
                if (arg7.getPackageManager().resolveActivity(v1_3, 0) == null) {
                    v1_3 = v2_2;
                }
                if (v1_3 != null) {
                    v1_3.putExtra("key_action", v4_1);
                    v1_3.putExtra("key_params", v3_1);
                    if ((arg7 instanceof Activity)) {
                        ((Activity) arg7).startActivityForResult(v1_3, b);
                    } else {
                        v1_3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        arg7.startActivity(v1_3);
                    }
                }

                v0 = true;
            } catch (Exception v1_2) {
                return false;
            }
        }

        return v0;
    }

    public static boolean b(Context arg1) {
        return a(arg1);
    }

    public static boolean b(Context arg8, String arg9, String arg10, String arg11, String arg12, String arg13, String arg14) {
        boolean v0_1;
        String v0 = c(arg8);
        if (android.support.design.a.isTextEmpty(v0)) {
            v0_1 = false;
            return v0_1;
        }

        String v1 = (android.support.design.a.isTextEmpty(arg9)) || (TTUtils.isValidUrl(arg9)) ? arg9 : "";
        if ((android.support.design.a.isTextEmpty(arg10)) && (android.support.design.a.isTextEmpty(arg11)) && (android.support.design.a.isTextEmpty(arg12)) || (android.support.design.a.isTextEmpty(v1))) {
            return false;
        }

        String v2 = (android.support.design.a.isTextEmpty(arg10)) || arg10.length() <= 200 ? arg10 : arg10.substring(0, 200) + "...";
        String v3 = (android.support.design.a.isTextEmpty(arg11)) || arg11.length() <= 600 ? arg11 : arg11.substring(0, 600) + "...";
        if (a(v0, "4.6.0") < 0) {
            int v7 = 0;
            if (a(v0, "4.5.0") >= 0) {
                v7 = 1;
            }

            return a(arg8, v1, v2, v3, arg12, arg13, arg14, v7);
        }

        StringBuilder v0_2 = new StringBuilder("mqqapi://share/to_qzone?src_type=app&version=1&file_type=news");
        if (!android.support.design.a.isTextEmpty(arg12)) {
            v0_2.append("&image_url=").append(Base64.encodeToString(arg12.getBytes(), 2));
        }

        if (!android.support.design.a.isTextEmpty(arg13)) {
            v0_2.append("&file_data=").append(Base64.encodeToString(arg13.getBytes(), 2));
        }

        if (!android.support.design.a.isTextEmpty(v2)) {
            v0_2.append("&title=").append(Base64.encodeToString(v2.getBytes(), 2));
        }

        if (!android.support.design.a.isTextEmpty(v3)) {
            v0_2.append("&description=").append(Base64.encodeToString(v3.getBytes(), 2));
        }

        v0_2.append("&share_id=").append(a);
        if (!android.support.design.a.isTextEmpty(v1)) {
            v0_2.append("&url=").append(Base64.encodeToString(v1.getBytes(), 2));
        }

        if (!android.support.design.a.isTextEmpty(arg14)) {
            if (arg14.length() > 20) {
                arg14 = arg14.substring(0, 20) + "...";
            }

            v0_2.append("&app_name=").append(Base64.encodeToString(arg14.getBytes(), 2));
        }

        v0_2.append("&req_type=" + Base64.encodeToString("1".getBytes(), 2));
        v0_2.append("&cflag=" + Base64.encodeToString("0".getBytes(), 2));
        v0 = v0_2.toString();
        Logger.v("shareToQQ", v0);
        try {
            Intent v1_1 = new Intent("android.intent.action.VIEW", Uri.parse(v0));
            if (!ToolUtils.isInstalledApp(arg8, v1_1)) {
                return false;
            }

            if ((arg8 instanceof Activity)) {
                ((Activity) arg8).startActivityForResult(v1_1, a(true));
            } else {
                v1_1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                arg8.startActivity(v1_1);
            }

            v0_1 = true;
        } catch (Exception v0_3) {
            v0_1 = false;
        }

        return v0_1;
    }

    private static String c(Context arg4) {
        String v0 = null;
        try {
            PackageInfo v1_1 = arg4.getPackageManager().getPackageInfo("com.tencent.mobileqq", 0);
            if (v1_1 == null) {
                return v0;
            }

            v0 = v1_1.versionName;
        } catch (Exception v1) {
        }

        return v0;
    }
}

